package com.star.carservice.client.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import com.star.carservice.R;
import com.star.carservice.client.activity.MainActivity;
import com.star.carservice.client.activity.RegisterActivity;
import com.star.carservice.client.activity.SplashActivity;
import com.star.carservice.client.fragment.base.BaseFragment;
import com.star.carservice.client.utils.PreferencesUtils;
import com.star.carservice.client.utils.StringUtils;
import com.star.carservice.client.utils.ToastUtils;
import com.star.carserviceapi.domain.request.LoginRequest;
import com.star.carserviceapi.domain.result.LoginResult;

public class LoginFragment extends BaseFragment {

	private final static String TAG = LoginFragment.class.getSimpleName();

	private final int RequestCodeLogin = 100;

	@InjectView(R.id.input_mobile)
	EditText input_mobile;

	@InjectView(R.id.input_pwd)
	EditText input_pwd;

	@Override
	public String getLogTag() {
		return TAG;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);

		if (CacheViewIsNull()) {
			mCacheView = inflater
					.inflate(R.layout.frag_login, container, false);

			ButterKnife.inject(this, mCacheView);
		}

		return mCacheView;
	}

	@OnClick(R.id.btn_register)
	public void register(View view) {
		Intent intent = new Intent(getActivity(), RegisterActivity.class);
		startActivityForResult(intent, RequestCodeLogin);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == RequestCodeLogin && resultCode == Activity.RESULT_OK) {
			// register ok

			gotoMain();
		}

		super.onActivityResult(requestCode, resultCode, data);

	}

	@OnClick(R.id.btn_login)
	public void login(View view) {
		String mobile = input_mobile.getText().toString().trim();
		String pwd = input_pwd.getText().toString().trim();
		if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(pwd)) {
			return;
		}

		LoginTask loginTask = new LoginTask();

		execAsyncTask(loginTask, new LoginRequest(mobile, pwd));
	}

	private String getMobile() {
		String mobile = input_mobile.getText().toString().trim();
		return mobile;
	}

	private void gotoMain() {
		startActivity(new Intent(getActivity(), MainActivity.class));
		finishActivity();
	}

	class LoginTask extends AsyncTask<LoginRequest, Void, LoginResult> {

		@Override
		protected LoginResult doInBackground(LoginRequest... params) {

			return getApi().login(params[0]);
		}

		@Override
		protected void onPostExecute(LoginResult result) {
			if (result.successful()) {
				PreferencesUtils.setUserId(result.getData().getUid());
				PreferencesUtils.setMobile(getMobile());
				PreferencesUtils.setAccessToken(result.getData()
						.getAccesstoken());

				gotoMain();
			} else {
				ToastUtils.show(getActivity(), result.getMessage());
			}
		}
	}

}
